package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.PlatOrganizationAttachment;
import com.hoshiicloud.user.param.organization.PlatOrganizationAttachmentAddParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationAttachmentQueryParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationAttachmentUpdateParam;
import com.hoshiicloud.user.service.organization.PlatOrganizationAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 机构附件 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Slf4j
@RestController
@RequestMapping("/organization/plat-organization-attachment")
@Api(value = "租户平台供应商附件", tags = {"租户平台供应商附件"})
@ApiSort(21)
public class PlatOrganizationAttachmentController {
    @Autowired
    private PlatOrganizationAttachmentService platOrganizationAttachmentService;

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 分页查询机构附件
    */
    @ApiOperation(value = "分页查询机构附件")
    @PostMapping("/getPage")
    public Response page(@RequestBody PlatOrganizationAttachmentQueryParam param) {
        try {
            QueryWrapper<PlatOrganizationAttachment> queryWrapper = new QueryWrapper<PlatOrganizationAttachment>();
            queryWrapper.lambda()
            .eq((param.getVendorId() != null&&param.getVendorId() !=0), PlatOrganizationAttachment::getVendorId, param.getVendorId())
            .eq(StringUtils.isNotBlank(param.getAttachName()), PlatOrganizationAttachment::getAttachName, param.getAttachName())
            .eq(StringUtils.isNotBlank(param.getAttachUrl()), PlatOrganizationAttachment::getAttachUrl, param.getAttachUrl())
            .eq(StringUtils.isNotBlank(param.getExpirationDate()), PlatOrganizationAttachment::getExpirationDate, param.getExpirationDate());
            Page<PlatOrganizationAttachment> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
            return Response.data(platOrganizationAttachmentService.getPage(page,queryWrapper,key));
          }catch (Exception e) {
            log.error("分页查询机构附件异常", e);
            return Response.failed("分页查询机构附件异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 根据id查询机构附件
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询机构附件")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(platOrganizationAttachmentService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增机构附件")
    public Response save(@RequestBody @Valid PlatOrganizationAttachmentAddParam param) {
        PlatOrganizationAttachment data;
        try {
             data = BeanUtils.covertType(param, PlatOrganizationAttachment.class);
        } catch (Exception e) {
            log.error("新增机构附件Bean转换异常", e);
            return Response.failed("新增机构附件Bean转换异常");
        }
        return Response.data(platOrganizationAttachmentService.save(data));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 修改机构附件
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改机构附件")
    public Response update(@RequestBody @Valid PlatOrganizationAttachmentUpdateParam param) {
        PlatOrganizationAttachment data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationAttachment.class);
        } catch (Exception e) {
            log.error("修改机构附件Bean转换异常", e);
            return Response.failed("修改机构附件Bean转换异常");
        }
        return Response.data(platOrganizationAttachmentService.updateById(data));
    }


    @PostMapping("/insertAttach4PlatOrganization")
    @ApiOperation(value = "批量新增租户供应商附件plat_organization_attachment")
    public Response insertAttach4PlatOrganization(@RequestBody @Valid List<PlatOrganizationAttachment> list){
        return Response.data(platOrganizationAttachmentService.insertAttach4PlatOrganization(list));
    }
}
